﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.ComponentModel;
using System.Configuration.Install;
using System.Collections;
using System.Diagnostics;
using System.Timers;

namespace EasySL.Server
{
    public class WindowsService : ServiceBase
    {
        public WindowsService(ServerApp app)
        {
            this.app = app;
        }

        public void Run()
        {
            ServiceBase.Run(this);
        }

        protected override void OnStart(string[] args)
        {
            //#if DEBUG
            //            Debugger.Launch();
            //#endif

            timerDelay = new Timer(30000);
            timerDelay.Elapsed += new System.Timers.ElapsedEventHandler(timerDelay_Elapsed);
            timerDelay.Start();

            this.app.Run();
        }

        void timerDelay_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            timerDelay.Enabled = false;
            timerDelay.Close();

          
        }

        protected override void OnStop()
        {

        }

        private ServerApp app = null;
        private Timer timerDelay = null;
    }


    [RunInstallerAttribute(true)]
    public class ProjectInstaller : Installer
    {
        ServiceProcessInstaller m_ProcessInstaller = null;
        ServiceInstaller m_ServiceInstaller = null;

        public ProjectInstaller()
        {
            this.m_ProcessInstaller = new ServiceProcessInstaller();
            this.m_ServiceInstaller = new ServiceInstaller();

            this.m_ProcessInstaller.Account = System.ServiceProcess.ServiceAccount.NetworkService;
            this.m_ProcessInstaller.Username = null;
            this.m_ProcessInstaller.Password = null;

            this.m_ServiceInstaller.StartType = ServiceStartMode.Automatic;
            this.m_ServiceInstaller.Description = "CodeMinePortalWatchDog Service";
            this.m_ServiceInstaller.DisplayName = "CodeMinePortalWatchDog";
            this.m_ServiceInstaller.ServiceName = "CodeMinePortalWatchDog";

            Installers.Add(this.m_ServiceInstaller);
            Installers.Add(this.m_ProcessInstaller);
        }

        protected override void OnBeforeInstall(IDictionary savedState)
        {
            base.OnBeforeInstall(savedState);

            SetInstallerParam();
        }

        protected override void OnBeforeUninstall(IDictionary savedState)
        {
            base.OnBeforeUninstall(savedState);

            SetInstallerParam();
        }

        private void SetInstallerParam()
        {
            // Partner name and service name will be passed as a parameter 
            // when calling "InstallUtil":
            // For example: InstallUtil /servicename=ODPService

            // we will use them to for installer parameters 

            string serviceName = GetContextParam("servicename");

            if (string.IsNullOrEmpty(serviceName))
                serviceName = "CodeMinePortalWatchDog";

            this.m_ServiceInstaller.Description = "CodeMinePortalWatchDog Service";
            this.m_ServiceInstaller.DisplayName = string.Format(serviceName);
            this.m_ServiceInstaller.ServiceName = string.Format(serviceName);
        }

        private string GetContextParam(string key)
        {
            string sValue = "";
            try
            {
                sValue = this.Context.Parameters[key].ToString();
                Console.WriteLine(sValue);
            }
            catch (Exception ex)
            {
                sValue = "";
                Console.WriteLine(ex.Message);
            }

            return sValue;
        }
    }
}
